﻿/********************************************************************************************
 * 文件名称:	CommonMenu.cs
 * 设计人员:	严(yanxianghui@gmail.com)
 * 设计时间:	2009-03-16
 * 功能描述:	栏目结构
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Shanfree.Framework.Utility;
using Shanfree.SharpMovie.IDAL;
using Shanfree.SharpMovie.Model;

namespace Shanfree.SharpMovie.SQLServerDAL
{
    /// <summary>
    /// 栏目结构
    /// </summary>
    public class Menu : DBPersistenceBase<MenuInfo>, IMenu
    {
        #region SQL
        private const string MENU_SELECT_ID = @"SELECT *
                                                                              FROM [dbo].[Menu]
                                                                              WHERE [ID] = @ID";
        private const string MENU_SELECT_ALL = @"SELECT *
                                                                              FROM [dbo].[Menu] 
                                                                              ORDER BY [SortNum] ASC";
        private const string MENU_SELECT_HOMEPAGE = @"SELECT *
                                                                              FROM [dbo].[Menu]
                                                                              WHERE [IsShow] = 1
                                                                              ORDER BY [SortNum] ASC";
        private const string MENU_SELECT_PARENTID = @"SELECT *
                                                                              FROM [dbo].[Menu]
                                                                              WHERE [ParentID] = @ParentID
                                                                              ORDER BY [SortNum] ASC";
        #endregion

        #region 数据库连接串
        /// <summary>
        /// 数据库连接串
        /// </summary>
        protected override string ConnectionString
        {
            get
            {
                return DBConnection.ConnectionString;
            }
        }
        #endregion

        #region IMenu 成员
        /// <summary>
        /// 根据根节点ID获取栏目树
        /// </summary>
        /// <param name="parentID">根节点ID，默认为-1</param>
        /// <param name="isAll">true:显示所有栏目；false:显示首页栏目</param>
        /// <returns></returns>
        public TreeNodeInfo<MenuInfo> GetMenuTree(string parentID, bool isAll)
        {
            string sql = (isAll == true) ? MENU_SELECT_ALL : MENU_SELECT_HOMEPAGE;
            SqlDataReader reader = SqlHelper.ExecuteReader(DBConnection.ConnectionString, CommandType.Text, sql);
            IList<MenuInfo> list = GetListFromReader(reader);
            // 添加虚拟的根节点
            list.Add(new MenuInfo(MenuInfo.DEFAULT_PARENT_ID));
            TreeNodeInfo<MenuInfo> tree = new TreeNodeInfo<MenuInfo>(list, parentID);
            return tree;
        }
        /// <summary>
        /// 获取子栏目列表
        /// </summary>
        /// <param name="parentID">父栏目ID</param>
        /// <returns></returns>
        public IList<MenuInfo> GetSubMenuList(string parentID)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@ParentID", parentID);
            SqlDataReader reader = SqlHelper.ExecuteReader(DBConnection.ConnectionString, CommandType.Text, MENU_SELECT_PARENTID,param);
            IList<MenuInfo> list = GetListFromReader(reader);
            return list;
        }
        #endregion
    }
}
